# 管理用户的登录状态
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
# 定义数据库
from app import db


# 用户
# class User(db.Model,UserMixin):
class User(db.Model, UserMixin):
    __tablename__ = 'User'
    # 用String不能用自增, 只能用db.Integer,
    UserID = db.Column(db.Integer, autoincrement=True, primary_key=True)
    UserName = db.Column(db.String(20), unique=True, index=True, nullable=False)
    password_hash = db.Column(db.String(128), nullable=False)
    UserMoney = db.Column(db.Float(), default=1000, nullable=False)
    UserAddress = db.Column(db.String(20))
    UserAvatarPath =  db.Column(db.String(30))

    def __repr__(self):
        # return '<User: %s %s %s %f>' % (self.UserID, self.UserName, self.UserPassword,self.UserMoney)
        # 则浏览器不能显示，因为浏览器不显示<>里面的东西，所以我改为【】
        return '【User: %s %s %s %f】' % (self.UserID, self.UserName, self.UserPassword,self.UserMoney)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def validate_password(self, password):
        return check_password_hash(self.password_hash, password)

    def get_id(self):
        return self.UserID

    @property
    def is_admin(self):
        return self.role.name == 'Administrator'


# 购物车
class BuyCar(db.Model):
    __tablename__ = 'BuyCar'
    BuyCarID = db.Column(db.Integer, autoincrement=True, primary_key=True)
    UserID = db.Column(db.Integer, db.ForeignKey('User.UserID'), primary_key=True)
    GoodsID = db.Column(db.Integer, db.ForeignKey('Goods.GoodsID'), primary_key=True)
    GoodsName = db.Column(db.String(30), nullable=False)
    GoodsPrice = db.Column(db.Float, nullable=False)
    BuyCarNumber = db.Column(db.Integer, nullable=False)
    TotalPrice = db.Column(db.Float, nullable=False)


# 类别（如水果、肉类、蔬菜）
class GoodsType(db.Model):
    __tablename__ = 'GoodsType'
    GoodsTypeName = db.Column(db.String(30), primary_key=True)


# 子类（如苹果、梨、猪肉）
class GoodsSubType(db.Model):
    __tablename__ = 'GoodsSubType'
    GoodsSubTypeName = db.Column(db.String(30), primary_key=True)
    GoodsTypeName = db.Column(db.String(30), db.ForeignKey('GoodsType.GoodsTypeName'), nullable=False)


# 商品（如苹果A、苹果B、猪肉A……）
class Goods(db.Model):
    __tablename__ = 'Goods'
    GoodsID = db.Column(db.Integer, autoincrement=True, primary_key=True)
    GoodsSubTypeName = db.Column(db.String(30), db.ForeignKey('GoodsSubType.GoodsSubTypeName'), nullable=False)
    GoodsName = db.Column(db.String(30),  nullable=False)
    GoodsPrice = db.Column(db.Float,  nullable=False)
    GoodsCount = db.Column(db.Integer, nullable=False)
    GoodsSell = db.Column(db.Integer, default=0)
    GoodsWeight = db.Column(db.Float)
    GoodsIntroduction = db.Column(db.String(200))
    GoodsImage=db.Column(db.String(20))


# 订单信息
class Order(db.Model):
    __tablename__ = 'Order'
    OrderID = db.Column(db.Integer, autoincrement=True, primary_key=True)
    UserID = db.Column(db.Integer, db.ForeignKey('User.UserID'), index=True, primary_key=True)
    CreateTime = db.Column(db.Date)
    FinishedTime = db.Column(db.Date)
    CancelTime = db.Column(db.Date)
    PaymentTime = db.Column(db.Date)
    SendTime=db.Column(db.Date)
    TotalPayment = db.Column(db.Float, default=0)


# 订单详情信息
class OrderItem(db.Model):
    __tablename__ = 'OrderItem'
    OrderID = db.Column(db.Integer, db.ForeignKey('Order.OrderID'), primary_key=True, index=True)
    GoodsID = db.Column(db.Integer, db.ForeignKey('Goods.GoodsID'), primary_key=True, index=True)
    GoodsPrice = db.Column(db.Float, nullable=False)
    GoodsName = db.Column(db.String(20), nullable=False)
    BuyNumber = db.Column(db.Integer, nullable=False)
    ItemPayment = db.Column(db.Float, nullable=False)